<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<title>新闻管理</title>
<%@ include file="/base/jsp/include.jsp"%>
</head>
<body>
	<div class="mini-toolbar" style="padding:2px;border-bottom:0;">
		<table style="width:100%;">
			<tr>
				<td style="width:100%;"><a class="mini-button"
					iconCls="icon-tip" plain="true" onclick="push()">推送</a> <span
					class="separator"></span><a class="mini-button"
					iconCls="icon-add" plain="true" onclick="addRow()">添加</a> <span
					class="separator"></span> <a class="mini-button"
					iconCls="icon-edit" plain="true" onclick="edit()">修改</a> <span
					class="separator"></span> <a class="mini-button"
					iconCls="icon-remove" plain="true" onclick="del()">删除</a><span
					class="separator"></span> <a class="mini-button"
					iconCls="icon-cancel" plain="true" href="javascript:clearFilter()">清除筛选</a></td>
			</tr>
		</table>
	</div>
	<span id="module-name" style="display:none;">news</span>
	<!--撑满页面-->
	<div class="mini-fit">
		<div id="grid" class="mini-datagrid" style="width:100%;height:100%;"
			pageSize="20" borderStyle="border:1;" url="listNews.do" idField="id"
			multiSelect="true" showFilterRow="true">
			<div property="columns">
				<div type="checkcolumn" field="activityId"></div>
				<div type="indexcolumn" headerAlign="center">序号</div>
				<div field="title" width="150" headerAlign="center" align="center">标题
				<input id="titleFilter" property="filter" class="mini-textbox" style="width:100%;" onvaluechanged="onFilterChanged"/></div>
				<div field="description" width="50" headerAlign="center" align="center">描述
				<input id="descFilter" property="filter" class="mini-textbox" style="width:100%;" onvaluechanged="onFilterChanged"/></div>
				<div name="date"  field="date" width="50" headerAlign="center" align="center"  dateFormat="yyyy-MM-dd">日期
				<input id="dateFilter" property="filter" class="mini-datepicker" format="yyyy-MM-dd" style="width:100%;" onvaluechanged="onFilterChanged"/></div>
				<div field="img" width="50" headerAlign="center" align="center" renderer="pictureHandler">图片</div>	
				<div field="source" width="50" headerAlign="center" align="center">来源
				<input id="sourceFilter" property="filter" class="mini-textbox" style="width:100%;" onvaluechanged="onFilterChanged"/></div>
				<div field="url" width="50" headerAlign="center" align="center" renderer="newsUrlHandler">访问地址</div>
				<div field="type" width="50" headerAlign="center" align="center" renderer="typeHandler">类别
				<input id="typeFilter" property="filter" class="mini-combobox" textField="text" valueField="id" emptyText="类别"
                        url="${path}/base/json/newsType.json" allowInput="false" showNullItem="true" nullItemText="请选择..." onvaluechanged="onFilterChanged"/>
				</div>
				<div field="showType" width="50" headerAlign="center" align="center" renderer="showTypeHandler">展示类别
				<input id="showTypeFilter" property="filter" class="mini-combobox" textField="text" valueField="id" emptyText="展示类别"
                        url="${path}/base/json/newsShowType.json" allowInput="false" showNullItem="true" nullItemText="请选择..." onvaluechanged="onFilterChanged"/>
				</div>
				<div field="top" width="50" headerAlign="center" align="center" renderer="topControlHandler">操作</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
	mini.parse();
	var grid = mini.get("grid");
	grid.load();	
	var rssData;
	function addRow() {
		mini.open({
			url : "toEditNews.do",
			title : "新闻新增",
			width : editorWinWidth,
			height : editorWinHeight,
			showMaxButton: Boolean,     //显示最大化按钮
			onload : function() {
				var iframe = this.getIFrameEl();
				var data = {
					action : "new"
				};
				iframe.contentWindow.SetData(data);
			},
			ondestroy : function(action) {
				grid.reload();
			}
		});
	}

	function del() {
		var rows = grid.getSelecteds();
		if (rows.length > 0) {
			mini.confirm("确定删除吗？", "确定？", function(action) {
				if (action == "ok") {
					var ids = [];
					for ( var i = 0, l = rows.length; i < l; i++) {
						var r = rows[i];
						ids.push(r.id);
					}
					var id = ids.join(",");
					var urls = [];
					for ( var i = 0, l = rows.length; i < l; i++) {
						var r = rows[i];
						urls.push(r.url);
					}
					var url = urls.join(",");
					grid.loading("操作中，请稍后......");
					$.ajax({
						url : "delNews.do?id=" + id + "&url=" + url,
						success : function(text) {
							grid.reload();
						},
						error : function() {
						}
					});
				}
			});
		} else {
			mini.alert("请选中一条记录");
		}
	}
	

	function edit() {
		var rows = grid.getSelecteds();
		if (rows.length == 1) {
			var row = grid.getSelected();
			mini.open({
				url : "toEditNews.do",
				title : "新闻编辑",
				width : editorWinWidth,
				height : editorWinHeight,
				showMaxButton: true,     //显示最大化按钮
				onload : function() {
					var iframe = this.getIFrameEl();
					var data = {
						action : "edit",
						id : row.id
					};
					iframe.contentWindow.SetData(data);
				},
				ondestroy : function(action) {
					grid.reload();
				}
			});
		} else if (rows.length > 1) {
			mini.alert("只能选一条记录");
		} else {
			mini.alert("请选中一条记录");
		}
	}

	function search() {
		var key = mini.get("key").getValue();
		grid.load({
			key : key.trim()
		});
	}
	
	function topSet(uid) {
       	mini.confirm("确定置顶吗？", "确定？", function(action) {
       		if(action == "ok") {
       			 $.ajax({
                   	url: "topSetNews.do?id=" + uid,
                   	cache: false,
                   	success: function (text) {
                   		if(text.indexOf('true') > -1){
                   			mini.alert("操作成功");
    				    	onFilterChanged();
    				    }else{
    				    	mini.alert("操作失败");
    				    }
                  		},
                  		error: function (jqXHR, textStatus, errorThrown) {
                  			mini.alert("置顶失败");
                   	}
               	});
       		}
       	});
       }
   function topDel(uid) {
       	mini.confirm("确定取消置顶吗？", "确定？", function(action) {
       		 if(action == "ok") {
       		 	$.ajax({
                   	url: "topDelNews.do?id=" + uid,
                   	cache: false,
                   	success: function (text) {
                   		if(text.indexOf('true') > -1){
    				    	mini.alert("操作成功");
    				    	onFilterChanged();
    				    }else{
    				    	mini.alert("操作失败");
    				    }
                  		},
                  		error: function (jqXHR, textStatus, errorThrown) {
                  			mini.alert("取消置顶失败");
                   	}
               	});
       		 }
       	});
       }
       
   function delTops() {
		var rows = grid.getSelecteds();
		if (rows.length > 0) {
			mini.confirm("确定取消置顶吗？", "确定？", function(action) {
				if (action == "ok") {
					var ids = [];
					for ( var i = 0, l = rows.length; i < l; i++) {
						var r = rows[i];
						ids.push(r.id);
						if(r.appid.indexOf("[") > -1 && appid != "all") {
							mini.alert("您无权限操作多个学校共有的新闻");
							return;
						}
					}
					var id = ids.join(",");
					var urls = [];
					for ( var i = 0, l = rows.length; i < l; i++) {
						var r = rows[i];
						urls.push(r.url);
					}
					var url = urls.join(",");
					grid.loading("操作中，请稍后......");
					$.ajax({
						url : "topDelNews.do?id=" + id,
						success : function(text) {
							grid.reload();
						},
						error : function() {
						}
					});
				}
			});
		} else {
			mini.alert("请选中一条记录");
		}
	}
       
   function onFilterChanged() {
      var title = mini.get("titleFilter").getFormValue();
      var desc = mini.get("descFilter").getFormValue();
      var source = mini.get("sourceFilter").getFormValue();
      var date = mini.get("dateFilter").getFormValue();
      var type = mini.get("typeFilter").getFormValue();
      var showType = mini.get("showTypeFilter").getFormValue();
       grid.load({title: title, description: desc, source : source,date:date,type:type,showType:showType});
   }
	
	function clearFilter() {
	  mini.get("titleFilter").setValue("");
	  mini.get("descFilter").setValue("");
	  mini.get("sourceFilter").setValue("");
	  mini.get("dateFilter").setValue("");
	  mini.get("typeFilter").setValue("");
	  mini.get("showTypeFilter").setValue("");
      grid.load();
   }
   
   var types = [
  	{id : 1,text : '普通新闻'}, 
  	{id : 2,text : '广告新闻'},
  	{id : 3,text : '笔记推荐'}
	];
	function typeHandler(e) {
		for ( var i = 0, l = types.length; i < l; i++) {
			var g = types[i];
			if (g.id == e.value)
				return g.text;
		}
		return "";
	}
	
	var showTypes = [
  		{id : 0,text : '列表展示'}, 
  		{id : 1,text : '大图展示'},
	];
	function showTypeHandler(e) {
		for ( var i = 0, l = showTypes.length; i < l; i++) {
			var g = showTypes[i];
			if (g.id == e.value)
				return g.text;
		}
		return "";
	}
	
	function newsUrlHandler(e) {
	    var tempUrl = e.value;
	    if(tempUrl.indexOf('http://') == -1) {
		    tempUrl = ossBaseURL + e.value;
	    }
	    return "<a class=\"New_Button\" href=\"" + tempUrl + "\" target=\"_blank\">点击访问</a>";
    }
   
    
   


	
</script>
</body>

</html>
